package co.yixiang.yshop.module.crm.controller.admin.crmbills;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageParam;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.util.object.BeanUtils;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.apilog.core.annotation.ApiAccessLog;
import static co.yixiang.yshop.framework.apilog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.crm.controller.admin.crmbills.vo.*;
import co.yixiang.yshop.module.crm.dal.dataobject.crmbills.CrmBillsDO;
import co.yixiang.yshop.module.crm.service.crmbills.CrmBillsService;

@Tag(name = "管理后台 - 账单")
@RestController
@RequestMapping("/crm/bills")
@Validated
public class CrmBillsController {

    @Resource
    private CrmBillsService billsService;

    @PostMapping("/create")
    @Operation(summary = "创建账单")
    @PreAuthorize("@ss.hasPermission('crm:bills:create')")
    public CommonResult<Long> createBills(@Valid @RequestBody CrmBillsSaveReqVO createReqVO) {
        return success(billsService.createBills(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单")
    @PreAuthorize("@ss.hasPermission('crm:bills:update')")
    public CommonResult<Boolean> updateBills(@Valid @RequestBody CrmBillsSaveReqVO updateReqVO) {
        billsService.updateBills(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:bills:delete')")
    public CommonResult<Boolean> deleteBills(@RequestParam("id") Long id) {
        billsService.deleteBills(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:bills:query')")
    public CommonResult<CrmBillsRespVO> getBills(@RequestParam("id") Long id) {
        CrmBillsDO bills = billsService.getBills(id);
        return success(BeanUtils.toBean(bills, CrmBillsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账单分页")
    @PreAuthorize("@ss.hasPermission('crm:bills:query')")
    public CommonResult<PageResult<CrmBillsRespVO>> getBillsPage(@Valid CrmBillsPageReqVO pageReqVO) {
        PageResult<CrmBillsDO> pageResult = billsService.getBillsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrmBillsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账单 Excel")
    @PreAuthorize("@ss.hasPermission('crm:bills:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBillsExcel(@Valid CrmBillsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CrmBillsDO> list = billsService.getBillsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账单.xls", "数据", CrmBillsRespVO.class,
                        BeanUtils.toBean(list, CrmBillsRespVO.class));
    }

}